//
//  ImageUtil.m
//  LOU
//
//  Created by iMacOne on 15/11/11.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "ImageUtil.h"
#import "Header.h"
//#import "DateUtil.h"
@implementation ImageUtil
#pragma mark 保持网络图片至本地
//+ (NSString*)saveImageLocal:(NSData*)data
//{
//    NSString * fileName = [NSString stringWithFormat:@"%@%d.png",[DateUtil formatDateNoStr:[NSDate date]],arc4random_uniform(10000)];
//    NSString * filePath = [FileUtil documentPath:fileName];
//    [data writeToFile:filePath atomically:YES];
//    return filePath;
//}

+ (UIView *)emptyDataBGView:(NSString *)imageName waringStr:(NSString *)waringStr
{
    UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, 60, WindowWidth, WindowHeight-60)];
    
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake((WindowWidth-70)/2, (WindowHeight-70)/2-100, 70, 70)];
    [vi addSubview:imageview];
    imageview.image = [UIImage imageNamed:imageName];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(imageview.frame)+30, WindowWidth, 20)];
    [vi addSubview:label];
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [ImageUtil colorWithRgb:0x999999];
    label.text = waringStr;
    label.textAlignment = 1;
    return vi;
}

+ (UIImage *)getImageFromURL:(NSString *)fileURL
{
    UIImage * result;
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result = [UIImage imageWithData:data];
    return result;
}
#pragma mark 将16进制颜色值转换为UIColor
+ (UIColor*) colorWithRgb:(int)color {
    return [UIColor colorWithRed:((Byte)(color >> 16))/255.0 green:((Byte)(color >> 8))/255.0 blue:((Byte)color)/255.0 alpha:1];
}
//对图片尺寸进行压缩--
+ (UIImage*)reduce:(UIImage*)image size:(CGSize)size isAdjust:(BOOL)isAdjust
{
    if (image == nil) {
        return nil;
    }
    if (image.size.width < size.width && image.size.height < size.height && isAdjust) {
        return image;
    }
    float sx = size.width / image.size.width, sy = size.height / image.size.height;
    if (isAdjust) {
        sx = (sx < sy ? sx : sy);
        sy = sx;
    }
    CGSize newSize = CGSizeMake(image.size.width * sx, image.size.height * sy);
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
// 根据view截图
+ (UIImage*)cutView:(UIView*)view
{
    return [ImageUtil cutView:view size:view.bounds.size];
}

// 根据view截图, 指定范围
+ (UIImage*)cutView:(UIView*)view size:(CGSize)size
{
    UIGraphicsBeginImageContextWithOptions(size, YES, 1);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
